package com.ggh.jinjilive.ui.home.live;

import androidx.databinding.ObservableField;
import androidx.lifecycle.MutableLiveData;

import com.ggh.jinjilive.mobile.ResultEntity;
import com.ggh.jinjilive.mobile.create.StartLiveMobile;
import com.ggh.jinjilive.mobile.home.live.LiveCategory;
import com.ggh.jinjilive.mobile.home.live.LiveList;
import com.ggh.jinjilive.remotedata.InterceptObserver;
import com.ggh.jinjilive.remotedata.systemapi.SystemApiService;
import com.ggh.jinjilive.ui.BaseViewModel;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class LiveViewModel extends BaseViewModel {
    //直播间类目列表
    public MutableLiveData<List<LiveCategory>> listMutableLiveData = new MutableLiveData<>();
    //娱乐 带货 红人 热门 才艺 游戏 体育
    public ObservableField<String> liveCategory = new ObservableField<>("1");
    //
    public ObservableField<String> liveType = new ObservableField<>("0");
    //直播间列表
    public MutableLiveData<List<LiveList>> liveData = new MutableLiveData<>();
    //进入直播间 接口
    public MutableLiveData<StartLiveMobile> mStartLiveMobile = new MutableLiveData<>();

    public LiveViewModel(SystemApiService apiCallInterface) {
        super(apiCallInterface);
    }

    /**
     * 直播间类目
     */
    public void liveCateGory(){
        Map<String, Object> paramsa = new HashMap<>();
        addSubscribe(apiCallInterface.liveCateGory(paramsa).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribeWith(new InterceptObserver<ResultEntity<List<LiveCategory>>>() {
            @Override
            public void onNextData(ResultEntity resultEntity) {
                List<LiveCategory> liveCategoryList = (List<LiveCategory>) resultEntity.getData();
                listMutableLiveData.setValue(liveCategoryList);
            }

            @Override
            public void onErrorNet(Throwable t, String netMsg) {

            }

        }));
    }

    /**
     * 直播间列表
     */
    public void liveList(){
        if (isRefresh) {
            page = 1;
        }else {
            page = page + 1;
        }
        Map<String, Object> paramsa = new HashMap<>();
        paramsa.put("category",liveCategory.get());
        paramsa.put("type",liveType.get());
        paramsa.put("page",page +"");
        paramsa.put("limit",pageSize);
        addSubscribe(apiCallInterface.liveList(paramsa).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribeWith(new InterceptObserver<ResultEntity<List<LiveCategory>>>() {
            @Override
            public void onNextData(ResultEntity resultEntity) {
                List<LiveList> liveCategoryList = (List<LiveList>) resultEntity.getData();
                liveData.setValue(liveCategoryList);
            }

            @Override
            public void onErrorNet(Throwable t, String netMsg) {
                liveData.setValue(null);
            }

        }));
    }


    /**
     * 进入直播间
     * @param live_id
     * @param pass 密码
     */
    public void theLiveView(int live_id,String pass){
        Map<String, Object> paramsa = new HashMap<>();
        paramsa.put("live_id",live_id);
        paramsa.put("pass",pass);
        addSubscribe(apiCallInterface.theLiveView(paramsa).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribeWith(new InterceptObserver<ResultEntity<StartLiveMobile>>() {
            @Override
            public void onNextData(ResultEntity resultEntity) {
                StartLiveMobile startLiveMobile = (StartLiveMobile) resultEntity.getData();
                mStartLiveMobile.setValue(startLiveMobile);
            }

            @Override
            public void onErrorNet(Throwable t, String netMsg) {
                liveData.setValue(null);
            }

        }));
    }
}
